//
//  DiscoverViewController.m
//  Coco_Music
//
//  Created by K。 on 15/6/19.
//  Copyright (c) 2015年 寇广超. All rights reserved.
//

#import "DiscoverViewController.h"
#import "DiscoverTableViewCell.h"
#import "MyScrollView.h"
#import "AFN_block.h"
#import "DiscoverDetailsController.h"
#import "NewSongViewController.h"
#import "NewSong.h"
#import "PlayingViewController.h"
#import "MJRefresh.h"
#import "UIScrollView+MJRefresh.h"
#import "DiscoverButtonController.h"
#import "LocalData.h"

@interface DiscoverViewController ()


@property(nonatomic,retain)DiscoverDetailsController *discoverDC;
@property(nonatomic,retain)UITableView *Mytable;
@property(nonatomic,retain)NewSongViewController *NewSong;
@property(nonatomic,retain)NSMutableArray *picUrl;
@property(nonatomic,retain)DiscoverButtonTableViewCell *cell;
@property(nonatomic,copy)NSString *name1;
@property(nonatomic,copy)NSString *name2;
@property(nonatomic,copy)NSString *name3;
@property(nonatomic,retain)NSMutableArray *TitleArray;
@property(nonatomic,retain)NSTimer *time;

@end

@implementation DiscoverViewController

-(void)dealloc
{
    [self.discoverDC release];
    [self.myTable release];
    [self.NewSong release];
    [self.picUrl release];
    [self.cell release];
    [self.name1 release];
    [self.name2 release];
    [self.name3 release];
    [self.TitleArray release];
    [self.allCellArray release];
    [self.allCollectionArray release];
    [self.allCollectionArray2 release];
    [self.allNewSongArray release];
    [self.allScrollArray release];
    [self.allType release];
    [self.scroll release];
    [super dealloc];
}


- (void)viewDidLoad {
     [super viewDidLoad];
    [self creastArray];
    UIImageView *image = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 64)];
    [[UIApplication sharedApplication]setStatusBarHidden:YES withAnimation:UIStatusBarAnimationSlide];
    
    
    [self.navigationController.navigationBar setBackgroundImage:image.image forBarMetrics:UIBarMetricsDefault];
    self.NewSong = [[NewSongViewController alloc]init];
    
    self.allScrollArray = [NSMutableArray array];
    self.allCollectionArray = [NSMutableArray array];
    self.allCellArray = [NSMutableArray array];
    self.allCollectionArray2 = [NSMutableArray array];
    self.TitleArray = [NSMutableArray array];
    
    [self GetDisvoberInfo];
    _Mytable = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height-44) style:UITableViewStyleGrouped];
    _Mytable.backgroundColor = [UIColor clearColor];
    self.view.backgroundColor = [UIColor clearColor];
    _Mytable.dataSource = self;
    _Mytable.delegate = self;
    _Mytable.showsVerticalScrollIndicator = NO;
    self.discoverDC = [[DiscoverDetailsController alloc]init];
    _scroll = [[MyScrollView alloc]initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height/5)];
    _Mytable.tableHeaderView = _scroll;
    [self.view addSubview:_Mytable];
    [_scroll release];
    
   [self.Mytable setSeparatorStyle:UITableViewCellSeparatorStyleNone];
    
    self.HUD = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    self.HUD.labelText = @"正在加载中,请稍后";
    [self.HUD show:YES];
    self.Mytable.header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(GetDisvoberInfo)];
    [self.discoverDC release];
    [self.Mytable release];
    [_NewSong release];
}


-(void)creastArray
{
    self.allScrollArray = [NSMutableArray array];
    self.allCollectionArray = [NSMutableArray array];
    self.allCellArray = [NSMutableArray array];
    self.allCollectionArray2 = [NSMutableArray array];
    self.TitleArray = [NSMutableArray array];
    self.NewSong = [[NewSongViewController alloc]init];
    [[LocalData shareLocalData]openLocalData];
    [[LocalData shareLocalData]CellButton2InfoTable];
    [[LocalData shareLocalData]CellButtonInfoTable];
    [[LocalData shareLocalData]CellInfoTable];
}


-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (self.allCollectionArray.count == 0) {
       
        if (section == 0) {
            return 2;
        }
        if (section == 1) {
            return 1;
        }
        if (section == 2) {
            return 1;
        }
    }
    else
    {
    if (section == 0) {
        return _allCellArray.count;
    }
    if (section == 1) {
        return 1;
    }
    if (section == 2) {
        return 1;

    }
    }
    return 0;
}


-(void)GetDisvoberInfo
{
    self.time = [NSTimer scheduledTimerWithTimeInterval:8 target:self selector:@selector(hideHUD) userInfo:nil repeats:NO];
    [AFN_block NSString:@"http://online.dongting.com/recomm/recomm_modules" Blokc:^(id request) {
        NSLog(@"111");
        [self.allCollectionArray removeAllObjects];
        [self.allCollectionArray2 removeAllObjects];
        [self.allCellArray removeAllObjects];
        [self.TitleArray removeAllObjects];
            [[LocalData shareLocalData]deleteCellButton];
            [[LocalData shareLocalData]deleteCellButton2];
            [[LocalData shareLocalData]deleteCellInfo];
            [[LocalData shareLocalData]deleteScrollpic];
//        if (request == nil) {
            [self.time invalidate];
//        }
//        else
//        {
            [self.HUD hide:YES];
            NSMutableDictionary *dic = [request objectForKey:@"data"];
            NSMutableArray *songlist = [dic objectForKey:@"songlists"];
            
            NSMutableDictionary *ScrollDic = [songlist firstObject];
            NSMutableArray *ScrollArray = [ScrollDic objectForKey:@"data"];
            
            NSMutableDictionary *cellDic = [songlist objectAtIndex:1];
            NSMutableArray *CellArray = [cellDic objectForKey:@"data"];
            _name1 = [cellDic objectForKey:@"name"];

            NSMutableDictionary *ButtonDic = [songlist objectAtIndex:2];
            NSMutableArray *ButtonArray = [ButtonDic objectForKey:@"data"];
            _name2 = [ButtonDic objectForKey:@"name"];
            
            NSMutableDictionary *collection = [songlist objectAtIndex:3];
            NSMutableArray *collectionArray = [collection objectForKey:@"data"];
            _name3 = [collection objectForKey:@"name"];
            [self.TitleArray addObject:_name1];
            [self.TitleArray addObject:_name2];
            [self.TitleArray addObject:_name3];
        
            NSMutableArray *picUrl = [NSMutableArray array];
            for (NSMutableDictionary *Scrolldic in ScrollArray) {
                Discover *discover = [[Discover alloc]init];
                discover.ScrollPic_url = [Scrolldic objectForKey:@"pic_url"];
                discover.ScrollValue = [Scrolldic objectForKey:@"value"];
                [picUrl addObject:[Scrolldic objectForKey:@"pic_url"]];
                [self.allScrollArray addObject:discover];
                [[LocalData shareLocalData]insert:discover];
                [discover release];
            }
        
            for (NSMutableDictionary *button in ButtonArray) {
                Discover *discover = [[Discover alloc]init];
                discover.Collection_Id = [button objectForKey:@"_id"];
                discover.CollectionName = [button objectForKey:@"name"];
                discover.CollectionPic_url = [button objectForKey:@"pic_url"];
                discover.CollectionId = [button objectForKey:@"id"];
                [_allCollectionArray addObject:discover];
                [[LocalData shareLocalData]insertCellButton:discover];
                [discover release];
            }
            
            for (NSMutableDictionary *collection in collectionArray) {
                Discover *discover = [[Discover alloc]init];
                if ([collection objectForKey:@"id"] == nil) {
                 NSMutableDictionary *dic = [collection objectForKey:@"action"];
                    discover.CollectionId = [dic objectForKey:@"value"];
                }
                else
                {
             discover.CollectionId = [collection objectForKey:@"id"];
                }
                discover.Collection_Id = [collection objectForKey:@"_id"];
                discover.CollectionName = [collection objectForKey:@"name"];
                discover.CollectionPic_url = [collection objectForKey:@"pic_url"];
  
                [_allCollectionArray2 addObject:discover];
                [[LocalData shareLocalData]insertCellButton2:discover];
                [discover release];
            }
            
            
            for (NSMutableDictionary *cell in CellArray) {
                Discover *discover = [[Discover alloc]init];
                discover.CellId = [cell objectForKey:@"_id"];
                discover.CellName = [cell objectForKey:@"name"];
                discover.CellPic_url = [cell objectForKey:@"pic_url"];
                discover.CellDesc = [cell objectForKey:@"desc"];
                [self.allCellArray addObject:discover];
                [[LocalData shareLocalData]insertCellInfo:discover];
                [discover release];
            }
        
            [self.Mytable.header endRefreshing];
            [_scroll setImages:picUrl];
            [_Mytable reloadData];
//        }
    }];
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (self.allCollectionArray.count == 0) {
        if (indexPath.section == 0)
        {
            static NSString *cellIdentifer = @"cellA";
            DiscoverTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifer];
            if (cell == nil) {
                cell = [[DiscoverTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifer];
            }
            cell.discover = [[[LocalData shareLocalData]GetCellInfo] objectAtIndex:indexPath.row];
            return cell;
        }
        if (indexPath.section == 1)
        {
            static NSString *cellIdentifer = @"cellB";
            self.cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifer];
            if (_cell == nil) {
                _cell = [[DiscoverButtonTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifer];
                _cell.Section = indexPath.section;
                _cell.array1 = [[LocalData shareLocalData]GetCellButton];
                self.cell.Mypushi = self;
            }
            return _cell;
        }
        if (indexPath.section == 2)
        {
            static NSString *cellIdentifer = @"cellC";
            self.cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifer];
            if (self.cell == nil) {
                self.cell = [[DiscoverButtonTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifer];
                _cell.Section = indexPath.section;
                _cell.array1 = [[LocalData shareLocalData]GetCellButton2];
                self.cell.Mypushi = self;

            }
            return _cell;
        }
    }
    else
    {
    if (indexPath.section == 0)
    {
        static NSString *cellIdentifer = @"cellA";
        DiscoverTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifer];
        if (cell == nil) {
            cell = [[DiscoverTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifer];
        }
        cell.discover = [self.allCellArray objectAtIndex:indexPath.row];
        return cell;
    }
    if (indexPath.section == 1)
    {
        static NSString *cellIdentifer = @"cellB";
        self.cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifer];
        if (_cell == nil) {
            _cell = [[DiscoverButtonTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifer];
            _cell.Section = indexPath.section;
            _cell.array1 = _allCollectionArray;
            self.cell.Mypushi = self;
        }
        self.cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return _cell;
    }
    if (indexPath.section == 2)
    {
        static NSString *cellIdentifer = @"cellC";
        self.cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifer];
        if (self.cell == nil) {
            self.cell = [[DiscoverButtonTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifer];
            _cell.Section = indexPath.section;
            _cell.array1 = _allCollectionArray2;
            self.cell.Mypushi = self;
        }
        self.cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return _cell;
    }
    }
    return nil;
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    if (self.allCollectionArray.count ==0 & [[LocalData shareLocalData] GetScrollPic].count != 0) {
    [self.scroll setImages:[[LocalData shareLocalData]GetScrollPic]];
   
    return [[LocalData shareLocalData]GetCellButton2].count;
    }
    
    return self.TitleArray.count;
}

-(void)hideHUD
{
    [self.HUD hide:YES];
    UIAlertView *alerview = [[UIAlertView alloc]initWithTitle:@"提示" message:@"亲,您的网络不给力,下拉一下再试试" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
    [self.Mytable.header endRefreshing];
    [alerview show];
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UILabel *label = [[UILabel alloc]init];
    label.font = [UIFont fontWithName:@"Menlo-Bold" size:17];
       if (self.TitleArray.count != 0) {
        label.text = [NSString stringWithFormat:@"  %@",[self.TitleArray objectAtIndex:section]];
        return label;
    }
    return nil;
   
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 30;
}

-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    if (self.allCollectionArray.count == 0) {
        
    }
    else
    {
    return [self.TitleArray objectAtIndex:section];
    }
    return nil;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section ==1) {
        return [UIScreen mainScreen].bounds.size.height/4;
    }
    if (indexPath.section ==2) {
        return [UIScreen mainScreen].bounds.size.height/4;
    }
    return [UIScreen mainScreen].bounds.size.height/7+10;
}




-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if (indexPath.section == 0&indexPath.row ==0) {
    [AFN_block NSString:@"http://online.dongting.com/recomm/new_songlists" Blokc:^(id request) {
        self.allNewSongArray = [NSMutableArray array];
       NSMutableArray *array = [request objectForKey:@"data"];
        for (NSMutableDictionary *dic in array) {
            NewSong *newsong = [[NewSong alloc]init];
            [newsong setValuesForKeysWithDictionary:dic];
            [self.allNewSongArray addObject:newsong];
        }
        self.NewSong.array = self.allNewSongArray;
    }];
    [self.navigationController pushViewController:_NewSong animated:YES];

    }
    if (indexPath.section ==0&indexPath.row ==1) {

        [self.navigationController pushViewController:_discoverDC animated:YES];
    }
    
}

-(void)pushDiscoverButton:(NSString *)Url;
{
    DiscoverButtonController *DBC = [[DiscoverButtonController alloc]init];
    DBC.url = Url;
    [self.navigationController pushViewController:DBC animated:YES];
    [DBC release];
}






- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
